/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0

ABOUT THIS NODE.JS EXAMPLE: This example works with Version 3 (V3) of the AWS SDK for JavaScript,
which is scheduled for release later in 2020. The prerelease version of the SDK is available
at https://github.com/aws/aws-sdk-js-v3. The 'SDK for JavaScript Developer Guide' for V3 is also
scheduled for release later in 2020, and the topic containing this example will be hosted at
https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/s3-example-creating-buckets.html.

Purpose:
s3_list1000plusObjects.js demonstrates how to list more than 1000 objects in an Amazon S3 bucket.

Inputs (replace in code):
- REGION
- BUCKET_NAME

Running the code:
node s3_list1000plusObjects.js
*/
// snippet-start:[s3.JavaScript.buckets.listManyObjectsV3]

// Import required AWS SDK clients and commands for Node.js
const { S3 } = require("@aws-sdk/client-s3");

// Set the AWS Region
const REGION = "REGION"; //e.g. "us-east-1"

// Create the parameters for the bucket
const bucketParams = { Bucket: "BUCKET_NAME" };

// Create S3 service object
const s3 = new S3(REGION);

async function run() {
    // Declare truncated as a flag that we will base our while loop on
    var truncated = true;
    // Declare a variable that we will assign the key of the last element in the response to
    var pageMarker;
    // While loop that runs until response.truncated is false
    while(truncated) {
        try {
            const response = await s3.listObjects(bucketParams);
            response.Contents.forEach(item => {
                console.log(item.Key);
            });
            // Log the Key of every item in the response to standard output
            truncated = response.IsTruncated;
            // If 'truncated' is true, assign the key of the final element in the response to our variable 'pageMarker'
            if (truncated) {
                pageMarker = response.Contents.slice(-1)[0].Key;
            }
            // At end of the list, response.truncated is false and our function exits the while loop.
        } catch(err) {
            console.log('Error', err)
        }
    }
}
run();
// snippet-end:[s3.JavaScript.buckets.listManyObjectsV3]
//for unit tests
exports.run = run();
